package com.ayu.chatroom.controller;


import com.ayu.chatroom.entity.ChatMsg;
import com.ayu.chatroom.entity.FriendsRequest;
import com.ayu.chatroom.entity.Users;
import com.ayu.chatroom.entity.vo.FriendRequestVo;
import com.ayu.chatroom.entity.vo.MyFriendVo;
import com.ayu.chatroom.entity.vo.TemperatureVo;
import com.ayu.chatroom.entity.vo.UserVo;
import com.ayu.chatroom.mapper.MyFriendsMapper;
import com.ayu.chatroom.service.ChatMsgService;
import com.ayu.chatroom.service.FriendsRequestService;
import com.ayu.chatroom.service.UsersService;
import com.ayu.chatroom.util.R;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ayu
 * @since 2022-10-10
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UsersController {


    private final UsersService usersService;

    private final FriendsRequestService friendsRequestService;

    private final MyFriendsMapper myFriendsMapper;

    private final ChatMsgService chatMsgService;


    @Autowired
    public UsersController(UsersService usersService, FriendsRequestService friendsRequestService, MyFriendsMapper myFriendsMapper, ChatMsgService chatMsgService) {
        this.usersService = usersService;

        this.friendsRequestService = friendsRequestService;
        this.myFriendsMapper = myFriendsMapper;
        this.chatMsgService = chatMsgService;
    }

    @PostMapping("registerOrLogin")
    @ApiOperation(value = "登陆或者注册")
    public R registerOrLogin(String username, String password, String cid) {

        //根据username去查询数据
        //存在就登陆，否则注册
        UserVo userVo = usersService.registerOrLogin(username, password, cid);
        if (Objects.nonNull(userVo)) {
            return R.ok().data(userVo);
        } else {
            return R.error().message("用户名或密码错误");

        }

    }


    @ApiOperation(value = "保存用户头像")
    @PostMapping(value = "uploadFaceBase64")
    public R uploadFaceBase64(String userId, String faceData) {

        Users user = usersService.uploadFaceBase64(userId, faceData);
        return R.ok().data(user);
    }

    @ApiOperation(value = "修改用户昵称")
    @PostMapping(value = "setNickname")
    public R setNickName(@RequestBody Users user) {

        usersService.updateById(user);
        Users byId = usersService.getById(user.getId());
        byId.setPassword("");
        return R.ok().data(byId);
    }


    @ApiOperation(value = "添加好友查询")
    @PostMapping(value = "searchFriend")
    public R searchFriend(String myUserId, String friendUserName) {

        //* 1.搜索的用户如果不存在，则返回【无此用户】
        //* 2.搜索的账号如果是你自己，则返回【不能添加自己】
        //* 3.搜索的朋友已经是你好友，返回【该用户已经是你的好友】

        return usersService.searchFriend(myUserId, friendUserName);
    }


    @ApiOperation(value = "发送添加好友请求")
    @PostMapping(value = "addFriendRequest")
    public R addFriendRequest(String myUserId, String friendUserId) {

        friendsRequestService.addRequest(myUserId, friendUserId);

        return R.ok().message("发送成功");

    }


    @ApiOperation(value = "接收遥测数据变化")
    @PostMapping(value = "acceptTimeseries")
    public R acceptTimeseries(@RequestBody TemperatureVo vo) {

        FriendsRequest friend = new FriendsRequest();

        friend.setAcceptUserId("遥测温度:" + vo.getTemperature());

        friend.setSendUserId("thingsboard");

        friend.setRequestDateTime(new Date());

        boolean save = friendsRequestService.save(friend);

        System.out.println("save = " + save);

        return R.ok().message("保存成功");
    }


    @ApiOperation("获取好友列表")
    @PostMapping(value = "myFriends")
    public R getFriendsList(String userId) {

        List<MyFriendVo> friendsList = myFriendsMapper.getFriendsList(userId);

        return R.ok().data(friendsList);
    }


    @ApiOperation("获取好友请求记录")
    @PostMapping("queryFriendRequest")
    public R queryFriendRequest(String userId) {

        List<FriendRequestVo> friendRequestList = myFriendsMapper.getFriendRequestList(userId);
        return R.ok().data(friendRequestList);
    }


    @ApiOperation("处理好友请求")
    @PostMapping("operFriendRequest")
    public R operFriendRequest(String acceptUserId, String sendUserId, Integer operType) {


        List<MyFriendVo> myFriendVos = usersService.operFriendRequest(acceptUserId, sendUserId, operType);

        return R.ok().data(myFriendVos);
    }


    /**
     * 用户手机端获取未签收的消息列表
     * @param acceptUserId acceptUserId
     * @return
     */
    @ApiOperation(value = "用户手机端获取未签收的消息列表")
    @PostMapping("/getUnReadMsgList")
    public R getUnReadMsgList(String acceptUserId){
        if(StringUtils.isBlank(acceptUserId)){
            return R.error().message("接收者ID不能为空");
        }
        //根据接收ID查找为签收的消息列表
        List<ChatMsg> unReadMsgList = chatMsgService.getUnReadMsgList(acceptUserId);
        return R.ok().data(unReadMsgList);

    }

}

